package com.sinosoft.dsp.platform.components.portal.input.elements;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.faces.model.SelectItem;

import org.hibernate.Query;

import com.sinosoft.builder.templates.concept.TagElementParameterType;
import com.sinosoft.builder.templates.concept.TagElementType;
import com.sinosoft.dsp.persistence.businessconcept.facade.ICodeFacade;
import com.sinosoft.dsp.platform.components.helper.TagElementHelper;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.platform.services.CodeSetService;
import com.sinosoft.dsp.platform.utils.SinoManagedBean;
import com.sinosoft.dsp.system.utils.Const;
import com.sinosoft.dsp.system.utils.DateUtils;

public class SelectOneTagElememt implements ISinoTagElement {

	private static final Log log = Log.getLog(SelectOneTagElememt.class
			.getName());

	public static final String PARAMETER_USE_CODE_ID = "useCodeId";

	public static final String PARAMETER_CODE_DATETIME = "codeDateTime";

	private Object value;

	private boolean useCodeId = true;

	private Date codeDateTime;

	private List<SelectItem> items;

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public List<SelectItem> getItems() {
		return items;
	}

	public void setItems(List<SelectItem> items) {
		this.items = items;
	}

	@Override
	public void setParameter(Query query, String key) {
		if (!isEmpty()) {
			if (value instanceof String) {
				query.setParameter(key, (String) value);
			} else if (value instanceof ICodeFacade) {
				if (useCodeId) {
					query.setParameter(key, ((ICodeFacade) value).getId());
				} else {
					if (codeDateTime != null) {
						Calendar cal = Calendar.getInstance();
						cal.setTime(codeDateTime);
						query.setParameter(key, ((ICodeFacade) value)
								.getValue(cal));
					} else {
						query.setParameter(key, ((ICodeFacade) value)
								.getValue());
					}
				}

			}
		}
	}

	@Override
	public boolean isEmpty() {
		if (value == null) {
			return true;
		}
		return false;
	}

	@Override
	public void initValue(TagElementType type) throws SinoPlatformException {
		try {
			if (type != null) {
				CodeSetService css = SinoManagedBean.findBean("codeSetService");
				// 初始化item
				if (!Const.isEmpty(type.getCodeSetId())) {
					items = css.getRootSubCodesSelectItems(type.getCodeSetId());

					TagElementParameterType useCodeIdParam = TagElementHelper
							.getTagElementParameter(type, PARAMETER_USE_CODE_ID);

					if (!TagElementHelper
							.isEmptyTagElementParameter(useCodeIdParam)) {
						this.useCodeId = Boolean.valueOf(useCodeIdParam
								.getValue());
					}

					TagElementParameterType codeDateTimeParam = TagElementHelper
							.getTagElementParameter(type,
									PARAMETER_CODE_DATETIME);

					if (!TagElementHelper
							.isEmptyTagElementParameter(codeDateTimeParam)) {
						this.codeDateTime = DateUtils.str2dat(codeDateTimeParam
								.getValue());
					}

				} else if (type.getTagElementParameter() != null) {
					items = new ArrayList<SelectItem>();
					for (TagElementParameterType tep : type
							.getTagElementParameter()) {
						items.add(new SelectItem(tep.getKey(), tep.getValue()));
					}
				}

				// 初始化value
				String tv = type.getValue();
				if (!Const.isEmpty(tv)) {
					if (!Const.isEmpty(type.getCodeSetId())) {
						value = css.getCode(type.getCodeSetId(), tv);
					} else {
						value = tv;
					}
				}
			}
		} catch (Exception e) {
			log.error("初始化单选框出现错误。");
			throw new SinoPlatformException("初始化单选框出现错误。");
		}
	}

}
